package com.itheima.controller;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/findAll")
    public String findAll() {
        List<User> userList = (List<User>) redisTemplate.boundListOps("user").leftPop();
        if (userList != null && userList.size() > 0) {
            return userList + "Redis查询数据成功";
        } else {
            List<User> list = userService.findAll();
            redisTemplate.boundListOps("user").leftPush(list);
            return list + "数据库查询数据成功";
        }
    }

    @RequestMapping("/add")
    public String add() {
        User user = new User("貂蝉", "闭月羞花");
        userService.add(user);
        return "新增数据成功";
    }

    @RequestMapping("/update")
    public String update() {
        User user = new User(13, "吕布", "马中赤兔,人中吕布");
        userService.update(user);
        return "更新数据成功";
    }

    @RequestMapping("/delete")
    public String delete() {
        int id = 14;
        userService.delete(id);
        return "删除数据成功";
    }
}
